from redis import asyncio as aioredis
from redis.exceptions import AuthenticationError, TimeoutError, RedisError
# from config.database import AsyncSessionLocal
# from module_admin.service.config_service import ConfigService
# from module_admin.service.dict_service import DictDataService
from utils.log_utils import logger
from dotenv import load_dotenv
import os

load_dotenv()


MYSQL_HOST = os.getenv("REDIS_HOST")
PASSWORD = os.getenv("REDIS_PASS")
REDIS_USER = os.getenv("REDIS_USER")


class RedisUtil:
    """
    Redis相关方法
    """

    @classmethod
    async def create_redis_pool(cls) -> aioredis.Redis:
        """
        应用启动时初始化redis连接

        :return: Redis连接对象
        """
        logger.info(f'开始连接redis...{MYSQL_HOST}')
        redis = await aioredis.from_url(
            url=f'redis://{MYSQL_HOST}',
            port="6379",
            # 默认没有账户
            # username=REDIS_USER,
            password=PASSWORD,
            # 数据库选择  0~15
            db=0,
            encoding='utf-8',
            decode_responses=True,
        )
        try:
            connection = await redis.ping()
            if connection:
                logger.info('redis连接成功')
            else:
                logger.error('redis连接失败')
        except AuthenticationError as e:
            logger.error(f'redis用户名或密码错误，详细错误信息：{e}，REDIS_USER:,{REDIS_USER},PASSWORD:,{PASSWORD}')
        except TimeoutError as e:
            logger.error(f'redis连接超时，详细错误信息：{e}')
        except RedisError as e:
            logger.error(f'redis连接错误，详细错误信息：{e}')
        return redis

    @classmethod
    async def close_redis_pool(cls, app):
        """
        应用关闭时关闭redis连接

        :param app: fastapi对象
        :return:
        """
        await app.state.redis.close()
        logger.info('关闭redis连接成功')

    @classmethod
    async def init_sys_dict(cls, redis):
        """
        应用启动时缓存字典表

        :param redis: redis对象
        :return:
        """
        # async with AsyncSessionLocal() as session:
        #     await DictDataService.init_cache_sys_dict_services(session, redis)

    @classmethod
    async def init_sys_config(cls, redis):
        """
        应用启动时缓存参数配置表

        :param redis: redis对象
        :return:
        """
        # async with AsyncSessionLocal() as session:
        #     await ConfigService.init_cache_sys_config_services(session, redis)
